package cn.edu.buu.hospital.service.impl;

import cn.edu.buu.hospital.entity.Report;
import cn.edu.buu.hospital.mapper.ReportMapper;
import cn.edu.buu.hospital.service.ReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public void add(Report report) {
        // 设置创建时间
        if (report.getCreatetime() == null) {
            report.setCreatetime(LocalDateTime.now());
        }
        reportMapper.insert(report);
    }

    @Override
    public void removeById(Long reportId) {
        reportMapper.deleteById(reportId);
    }

    @Override
    public void modify(Report report) {
        reportMapper.update(report);
    }

    @Override
    public Report findById(Long reportId) {
        return reportMapper.selectById(reportId);
    }

    @Override
    public List<Report> findAll() {
        return reportMapper.selectAll();
    }

    @Override
    public PageInfo<Report> findByUserId(int pageNum, int pageSize, Long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Report> reportList = reportMapper.selectByUserId(userId);
        return new PageInfo<>(reportList);
    }

    @Override
    public PageInfo<Report> find(int pageNum, int pageSize, Report condition) {
        PageHelper.startPage(pageNum, pageSize);
        List<Report> reportList = reportMapper.selectByCondition(condition);
        return new PageInfo<>(reportList);
    }
}